#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
	DWORD dwExitCode;
	HANDLE hndl;
	int argc_tmp;
	int if_dump_pid;
	char sleep_second[10];
	char sleep_second_tag[2];
	char cmd[500];
	if_dump_pid=0;
	argc_tmp=argc;
	memset(cmd,'\0',strlen(cmd));
	strncpy(cmd,"cmd /C ",7);
	while(argc_tmp--)
	{
		
		strncpy(sleep_second_tag, argv[argc_tmp], 2); 
		if(strcmp(sleep_second_tag,"-t")==0)
		{
			
			strncpy(sleep_second,argv[argc_tmp]+2,strlen(argv[argc_tmp])-2);
		}
		
	}

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    if( argc < 2)
    {
        printf("Usage: %s [-t[second]][cmdline]\n\t -e", argv[0]);
        return;
    }
	//printf("%s,%s\n",cmd,argv[argc-1]);
	strcpy(cmd+7,argv[argc-1]);
	printf("%s\n",cmd);
    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        cmd,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }
	
	Sleep(atoi(sleep_second)*1000);
	GetExitCodeProcess(pi.hProcess,&dwExitCode);
	hndl=OpenProcess(1,0,pi.dwProcessId);
	if(0==TerminateProcess(hndl,dwExitCode))
	{
		printf("%d:%s\n",GetLastError(),"Process Security and Access Rights");
	}
	if(GetLastError()!=0){
		printf( "TerminateProcess failed (%d).\n", GetLastError() );
	}
	
    
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
	
	
	
	

}